package com.isdepci.project.carbon.service.impl;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.BBlastEmission;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.mapper.BBlastEmissionMapper;
import com.isdepci.project.carbon.service.IBBlastEmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 爆破过程排放Service业务层处理
 *
 * @author isdepci
 * @date 2025-03-29
 */
@Service
public class BBlastEmissionServiceImpl implements IBBlastEmissionService
{
    @Autowired
    private BBlastEmissionMapper bBlastEmissionMapper;


//    @Override
//    public AjaxResult calBlastEmission(Long projectId) {
//        // 查询出所有的BNitreEmission数据
//        List<BBlastEmission> bBlastEmissions = bBlastEmissionMapper.selectBBlastEmissionByProjectId(projectId);
//        // 初始化总的排放量
//        BigDecimal totalEmission = BigDecimal.ZERO;
//
//        // 遍历每一条数据，计算排放量
//        for (BBlastEmission blastEmission : bBlastEmissions) {
//            // 计算排放量 EF = consumption * emissionFactor
//            BigDecimal emission = blastEmission.getConsumption()
//                    .multiply(blastEmission.getEmissionFactor());
//
//            // 累加排放量
//            totalEmission = totalEmission.add(emission);
//        }
//        // 返回总排放量
//        return AjaxResult.success(totalEmission.setScale(2, RoundingMode.HALF_UP));
//    }

    @Override
    public AjaxResult calBlastEmission(Long projectId) {
        // 查询出所有的BBlastEmission数据
        List<BBlastEmission> bBlastEmissions = bBlastEmissionMapper.selectBBlastEmissionByProjectId(projectId);
        BigDecimal totalEmission = BigDecimal.ZERO;
//        List<BigDecimal> emissionList = new ArrayList<>();
        // 遍历每一条数据，计算排放量
        for (BBlastEmission blastEmission : bBlastEmissions) {
            // 计算排放量 EF = consumption * emissionFactor
            BigDecimal emission = blastEmission.getConsumption()
                    .multiply(blastEmission.getEmissionFactor());
            blastEmission.setCarbonEmissions(emission);
            // 更新数据库中的排放量数据
            bBlastEmissionMapper.updateBBlastEmission(blastEmission);
//            emissionList.add(emission);
            // 累加总排放量s
            totalEmission = totalEmission.add(emission);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("totalEmission", totalEmission.setScale(2, RoundingMode.HALF_UP));
//        result.put("emissionList", emissionList);
        // 返回结果
        return AjaxResult.success(result);
    }



    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<BBlastEmission> list =  excel.getBlastEmissionList();
        list.forEach(item->{
            if(excel.getProjectId()!=null){
                item.setProjectId(excel.getProjectId());
            }
        });
        if(list.size()>0){
            bBlastEmissionMapper.insertList(list);
        }
        return AjaxResult.success();
    }




    /**
     * 查询爆破过程排放
     */
    @Override
    public BBlastEmission selectBBlastEmissionById(Long id)
    {
        return bBlastEmissionMapper.selectBBlastEmissionById(id);
    }

    /**
     * 查询爆破过程排放列表
     */
    @Override
    public List<BBlastEmission> selectBBlastEmissionList(BBlastEmission bBlastEmission)
    {
        return bBlastEmissionMapper.selectBBlastEmissionList(bBlastEmission);
    }

    /**
     * 新增爆破过程排放
     */
    @Override
    public int insertBBlastEmission(BBlastEmission bBlastEmission)
    {
        return bBlastEmissionMapper.insertBBlastEmission(bBlastEmission);
    }

    /**
     * 修改爆破过程排放
     */
    @Override
    public int updateBBlastEmission(BBlastEmission bBlastEmission)
    {
        return bBlastEmissionMapper.updateBBlastEmission(bBlastEmission);
    }

    /**
     * 批量删除爆破过程排放
     */
    @Override
    public int deleteBBlastEmissionByIds(Long[] ids)
    {
        return bBlastEmissionMapper.deleteBBlastEmissionByIds(ids);
    }

    /**
     * 删除爆破过程排放信息
     */
    @Override
    public int deleteBBlastEmissionById(Long id)
    {
        return bBlastEmissionMapper.deleteBBlastEmissionById(id);
    }


}
